using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using Microsoft.AceEngineering.SPM.SPMCustomDataEntity;
using Microsoft.ApplicationBlocks.Data;

namespace Microsoft.AceEngineering.SPM.SPMDataLayer
{
    public class ContactTypesDL
    {
        public static List<ContactTypeCDE> GetContactTypes(string dir, string stProcedure)
        {
            SqlDataReader sqlreader = null;
            List<ContactTypeCDE> contactTypeList = new List<ContactTypeCDE>();
            ContactTypeCDE contactType = null;

            try
            {
                string conString = ConfigurationManager.ConnectionStrings["SPMSQL"].ConnectionString;

                SqlParameter[] myParams = new SqlParameter[1];
                myParams[0] = new SqlParameter("@dir", SqlDbType.VarChar, 10);
                myParams[0].Value = GiveMeDir(dir);

                sqlreader = SqlHelper.ExecuteReader(conString, CommandType.StoredProcedure, stProcedure, myParams);

                while (sqlreader.Read())
                {
                    contactType = new ContactTypeCDE();
                    contactType.ContactTypeID = sqlreader.GetValue(0).ToString();
                    contactType.ContactType = sqlreader.GetValue(1).ToString();

                    contactTypeList.Add(contactType);
                    contactType = null;
                }
            }
            finally
            {
                if (sqlreader != null)
                {
                    if (!sqlreader.IsClosed)
                        sqlreader.Close();
                }
            }
            return contactTypeList;
        }

        public static void ChangeContactTypeStatus(string contactTypeID, bool status)
        {
            string conString = ConfigurationManager.ConnectionStrings["SPMSQL"].ConnectionString;

            SqlParameter[] myParams = new SqlParameter[2];
            myParams[0] = new SqlParameter("@ContactTypeID", SqlDbType.VarChar, 250);
            myParams[0].Value = contactTypeID;

            myParams[1] = new SqlParameter("@active", SqlDbType.Bit);
            myParams[1].Value = status;

            SqlHelper.ExecuteNonQuery(conString, CommandType.StoredProcedure, "SPM_spUpdateContactTypeStatus", myParams);
        }

        public static void InsertContactType(string contactType, string createdBy)
        {
            string conString = ConfigurationManager.ConnectionStrings["SPMSQL"].ConnectionString;

            SqlParameter[] myParams = new SqlParameter[2];
            myParams[0] = new SqlParameter("@ContactType", SqlDbType.VarChar, 50);
            myParams[0].Value = contactType;

            myParams[1] = new SqlParameter("@CreatedBy", SqlDbType.VarChar, 50);
            myParams[1].Value = createdBy;

            SqlHelper.ExecuteNonQuery(conString, CommandType.StoredProcedure, "SPM_spInsertContactType", myParams);
        }

        public static void UpdateContactType(string contactTypeID, string contactType, string createdBy)
        {
            string conString = ConfigurationManager.ConnectionStrings["SPMSQL"].ConnectionString;

            SqlParameter[] myParams = new SqlParameter[4];
            myParams[0] = new SqlParameter("@ContactTypeID", SqlDbType.VarChar, 50);
            myParams[0].Value = contactTypeID;

            myParams[1] = new SqlParameter("@ContactType", SqlDbType.VarChar, 50);
            myParams[1].Value = contactType;

            myParams[2] = new SqlParameter("@CreatedBy", SqlDbType.VarChar, 50);
            myParams[2].Value = createdBy;

            myParams[3] = new SqlParameter("@Active", SqlDbType.VarChar, 50);
            myParams[3].Value = true;

            SqlHelper.ExecuteNonQuery(conString, CommandType.StoredProcedure, "SPM_spUpdateContactType", myParams);
        }

        private static string GiveMeDir(string sortExp)
        {
            if (sortExp.Contains("DESC"))
                return "DESC";
            else
                return "ASC";
        }
    }
}